package com.lincoln.gateway.zuul;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author admin
 */
public class MyRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {
    private static Logger logger = LoggerFactory.getLogger(MyRouteLocator.class);

    /**
     * 当前线程的url
     */
    public static ThreadLocal<String> url = new ThreadLocal<>();

    /**
     * 配置集合
     */
    public static Map<String, Map<String, ZuulProperties.ZuulRoute>> maps = new ConcurrentHashMap<>();

    public MyRouteLocator(String servletPath, ZuulProperties properties) {
        super(servletPath, properties);
    }

    @Override
    public void refresh() {
        doRefresh();
    }

    @Override
    protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
        Map<String, ZuulProperties.ZuulRoute> zuulRouteLinkedHashMap = super.locateRoutes();


        if (url.get() == null) {
            logger.info(url.get() + "请求进入");
            return new ConcurrentHashMap<>(0);
        }
        if (maps.get(url.get()) == null) {
            logger.info(url.get() + "请求进入");
            return new ConcurrentHashMap<>(0);
        }
        logger.info(url.get() + "请求进入");
        return maps.get(url.get());
    }
}
